<?php

/**
 * MyProjectNameHere <http://www.example.com>
 * Model Class
 *
 * It is recommended to extend Model classes from WWW_Factory in order to 
 * provide various useful functions and API access for the Model.
 *
 * @package    Factory
 * @author     DeveloperNameHere <email@example.com>
 * @copyright  Copyright (c) 2012, ProjectOwnerNameHere
 * @license    Unrestricted
 * @tutorial   /doc/pages/guide_mvc.htm
 * @since      1.0.0
 * @version    1.0.0
 */

class WWW_model_users extends WWW_Factory {

	/**
	 * It is recommended to define all data variables here. Usually the 
	 * data variables have the same names as the column names of database 
	 * rows from a table.
	 */	
	public $conn;
	/**
	 * Alternative to __construct()
	 *
	 * WWW_Factory does not allow to overwrite __construct() method, so 
	 * this __initialize() is used instead and loaded automatically when 
	 * object is created.
	 *
	 * @return boolean
	 */
	public function __initialize(){
		/* require('engine/class.www-database.php');
		$this->conn=new WWW_Database(); */				
		// Do something here
		require_once('engine/include_dao.php');
		return true;
		
	}
	
	/**
	 * This is intended to load data from database
	 *
	 * @param integer $id identifier of the object loaded
	 * @return boolean
	 */
	
	public function loginuser(){		
		$data=$this->conn->dbSingle("SELECT * FROM `user_login` where email='".$_POST['username']."' and password='".$_POST['password']."'");		
		if($data){
			return array('message'=>'login Success','user_id'=>$data['user_id'],'email'=>$data['email']);
			//return $lastId=$db->dbLastId();
		}else{
			return array('message'=>'Login Failed');
		}	
	}
	public function createuser(){
		
		$password = rand(999,9999);
		$loginCreation=$this->conn->dbCommand("INSERT INTO `user_login` (`email`, `password`, `created`, `status`) VALUES ('".$_POST['email']."', '".$password."', NOW(), '1')");	
		//return array('msg'=>"INSERT INTO `user_login` (`email`, `password`, `created`, `status`) VALUES (NULL, '".$_POST['email']."', '".$password."', NOW(), '1')");
	if($loginCreation){
		 $userId=$this->conn->dbLastId();
		$result=$this->conn->dbCommand("INSERT INTO registeration(user_id,`first_name`, `last_name`, `email`, `phone`, `city_name`, `state_id`, `country_id`, `address`, `fax`, `company`, `photo`, `zip`, `created_by`, `create_date`,  `status`) VALUES ('".$userId."','".$_POST['first_name']."', '".$_POST['last_name']."', '".$_POST['email']."', '".$_POST['phone']."', '".$_POST['city_name']."', '".$_POST['state_id']."', '', '".$_POST['address']."','".$_POST['fax']."','".$_POST['company']."','', '".$_POST['zip']."', '1', NOW(),'1')");		
		if($result)
		return array('message'=>"Successfully Created",'password'=>$password,'username'=>$_POST['email']);
		else
		return array('message'=>'Registration Failed','password'=>$password,'username'=>$_POST['email']);
	}else{
		return array('message'=>'Login Failed','name'=>$_POST['first_name']);
	}
	
	}
	public function updateuser(){
		//return $_POST;
		
		extract($_POST);		
		$result=$this->conn->dbCommand("update registeration set `first_name`='".$first_name."',`last_name`='".$last_name."',`email`='".$email."', `phone`='".$phone."', `city_name`='".$city_name."', `state_id`=".$state_id.", `country_id`=".$country_id.", `address`='".$address."', `fax`='".$fax."', `company`='".$company."', `photo`='".$photo."', `zip`='".$zip."', `updated_by`=".$user_id.", `updated_date`= NOW() where user_id=".$user_id);
		if($result)
		return array('message'=>"Successfully Updated");
		else
		return array('message'=>'Updates Failed');
		
	}
	public function disableuser(){
		extract($_POST);		
		$result=$this->conn->dbCommand("update user_login set status='".$status."',`updated_by`=".$user_id.", `updated_date`= NOW() where user_id=".$disable_userid);
		if($result)
		return array('message'=>"Successfully Disabled");
		else
		return array('message'=>'Disable Failed');
	}
	public function changepassword(){
		extract($_POST);				
		$result=$this->conn->dbCommand("update user_login set password='".$newPassword."',`updated_by`=".$user_id.", `updated_date`= NOW() where user_id=".$user_id." and password='".$oldPassword."'");
		if($result)
		return array('message'=>"Successfully Updated");
		else
		return array('message'=>'updates Failed');
	}
	public function displayuser(){
		extract($_POST);		
		$result=$this->conn->dbSingle("select * from registeration where user_id=".$user_id);
		if($result)
		return array('message'=>"User Details",'user_info'=>$result);
		else
		return array('message'=>'User Failed');
	}
	public function getallusers(){
		$result=$this->conn->dbMultiple("select * from registeration");
		if($result){
			$totalRows=$this->conn->dbSingle('SELECT FOUND_ROWS() as rows;');
		return array('message'=>"All User",'user_info'=>$result,'total'=>$totalRows);
		}
		else
		return array('message'=>'Users Failed');
	}
	
	
}
	
?>